<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String ctx = request.getContextPath();
    request.setAttribute("ctx", ctx);
%>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/fileupload.js"></script>
    <link rel="stylesheet" href="css/img.css">
    <script>
        $(function (){
            // 课程列表
            $.ajax({
                url:"courseList.do",
                type:"post",
                dataType:"json",
                success:function (obj){
                    for (let x in obj) {
                        $("#courses").append(" <input type='checkbox' name='cids' value="+obj[x].cid+">"+obj[x].cname)
                    }
                }
            })

            $.ajax({
                url:"cityList.do",
                type:"post",
                data:{"pid":0},
                dataType:"json",
                success:function (obj){
                    for (let x in obj) {
                        $("[name='sheng.id']").append(" <option value="+obj[x].id+">"+obj[x].name+"</option>")
                    }
                }
            })
            $("[name='sheng.id']").change(function (){
                var pid=$(this).val()
                $("[name='shi.id']").empty();
                $("[name='shi.id']").append("<option value=''>请选择</option>")
                $("[name='xian.id']").empty();
                $("[name='xian.id']").append("<option value=''>请选择</option>")
                $.ajax({
                    url:"cityList.do",
                    type:"post",
                    data:{"pid":pid},
                    dataType:"json",
                    success:function (obj){
                        for (let x in obj) {
                            $("[name='shi.id']").append(" <option value="+obj[x].id+">"+obj[x].name+"</option>")
                        }
                    }
                })
            })

            $("[name='shi.id']").change(function (){
                var pid=$(this).val()
                $("[name='xian.id']").empty();
                $("[name='xian.id']").append("<option value=''>请选择</option>")
                $.ajax({
                    url:"cityList.do",
                    type:"post",
                    data:{"pid":pid},
                    dataType:"json",
                    success:function (obj){
                        for (let x in obj) {
                            $("[name='xian.id']").append(" <option value="+obj[x].id+">"+obj[x].name+"</option>")
                        }
                    }
                })
            })
        })

        function add(){
            var formData = new FormData($("form")[0]);
            $.ajax({
                url:"add.do",
                data:formData,
                type:"post",
                dataType:"json",
                processData:false,
                contentType:false,
                success:function (obj){
                    if(obj>0){
                        alert("OK");
                        location="list.do";
                    }
                }
            })
        }
    </script>
</head>
    <form action="add.do" method="post" >
        姓名:<input type="text" name="sname"><br>
        性别:<input type="radio" name="sex" value="男">男
        <input type="radio" name="sex" value="女">女<br>
        生日:<input type="text" name="birthday"><br>
        爱好:<input type="checkbox" name="hobby" value="抽烟">抽烟
        <input type="checkbox" name="hobby" value="喝酒">喝酒
        <input type="checkbox" name="hobby" value="烫头">烫头<br><br><br><br>
        图片:<div class="photoUpLoad" >
            <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
            <img src="" alt="" class="photoEcho">
        </div><br>
        地址:<select name="sheng.id">
        <option value="">请选择</option>
    </select>省
        <select name="shi.id">
            <option value="">请选择</option>
        </select> 市
        <select name="xian.id">
            <option value="">请选择</option>
        </select>县
        <div id="courses">

        </div>
    </form>
<input type="button"value="提交" onclick="add()">
</body>
</html>
